package com.gm.wholesale.module.order.ui.needOrder;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.gm.commonlib.base.baseui.baseactivity.BaseWebViewActivity;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.module.print.PrintUtil;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.BuildConfig;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.order.bean.needorder.NeedOrderDetailBean;
import com.gm.wholesale.module.user.UserDataManger;

public class PrintWebViewActivity extends BaseWebViewActivity {
    private NeedOrderDetailBean orderDetailBean;



    @Override
    public void onCreateTitle(FrameLayout mTitle) {
        super.onCreateTitle(mTitle);
        LayoutInflater.from(context).inflate(R.layout.view_print_header, mTitle);
        findViewById(R.id.backImage).setOnClickListener(v -> finish());
        findViewById(R.id.printTv).setOnClickListener(v -> {
            try {
                PrintUtil.createWebPrintJob(getActivity(), mViewView, result -> {
                    if (result){ //打印成功发送给打印机了，默认表示打印了,改变订单状态
                        EvenBusUtil.updateNeedOrderByPrint();
                    }
                    else { //失败关闭界面
                        finish();
                    }

                });
            }
            catch (Exception e){
                ToastUtil.showToast("设备不支持打印");
            }

        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderDetailBean = (NeedOrderDetailBean) getIntent().getSerializableExtra("data");
        String url = BuildConfig.WEB_HOST + "order_receive_print?needsId=" + orderDetailBean.getNeedsID() + "&token=" + UserDataManger.newInstance().getToken();
        loadUrl(url);

    }

}
